Repository navigation
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
lexical
@lexical/a11y
@lexical/clipboard
@lexical/code
@lexical/code-core
@lexical/code-prism
@lexical/code-shiki
@lexical/compiler
@lexical/devtools-core
@lexical/dragon
@lexical/eslint-plugin
@lexical/extension
@lexical/fast-check
@lexical/file
@lexical/hashtag
@lexical/headless
@lexical/history
@lexical/html
@lexical/internal
@lexical/link
@lexical/list
@lexical/mark
@lexical/markdown
@lexical/mdast
@lexical/offset
@lexical/overflow
@lexical/plain-text
@lexical/react
@lexical/rich-text
@lexical/selection
@lexical/table
@lexical/tailwind
@lexical/text
@lexical/utils
@lexical/y
@lexical/yjs
commit: |
There was a problem hiding this comment.
Thanks for tackling #3761! The fix works for the reported case: in the playground poll, Ctrl+Backspace in an option input now deletes a word instead of being swallowed by the editor, and the new browser test cleanly separates the old and new behavior. One issue needs addressing before this can merge.
Undo, redo and select all stop working when a non-text control inside a decorator has focus (packages/lexical/src/LexicalEvents.ts:2109, also :2191 and :2228)
isEventTargetingCapturedSelection returns true for any element inside a decorator, because the reconciler marks every decorator's DOM with setDOMUnmanaged(dom, {captureSelection: true}). So the new early return also fires when focus is on a <button> or a checkbox, where the browser has no default action for Mod+Z, Mod+Shift+Z, Ctrl+Y or Mod+A, and those keys now do nothing. To reproduce in the playground with Chrome: insert a poll, click Add Option, then press Ctrl+Z. On main the added option is undone; with this PR nothing happens. The description scopes the change to a decorator's input, and AGENTS.md's Backwards Compatibility section asks that existing behavior be kept intact otherwise.
Suggested fix: skip only for text-entry targets (a <textarea>, a text-type <input>, or a contentEditable that Lexical doesn't own), similar to how PreventSelectAllExtension limits its skip to INPUT/TEXTAREA targets (packages/lexical-extension/src/PreventSelectAllExtension.ts). A test case with a <button> inside the decorator, asserting that Mod+Z still dispatches UNDO_COMMAND, would lock this in.
Optional, non-blocking:
- The Mod+C/Mod+X cases only check that the keydown isn't cancelled. With real key presses, Ctrl+X in the input of a node-selected decorator still removes the decorator, through the native
cutevent and the rich-text cut handler. Since the description leaves the clipboard events to #9288, consider naming those two tests after what they assert. - AGENTS.md (Commits and Pull Requests) asks the Test plan's After section to paste the real command output rather than summarize it.
Keydown shortcuts that preventDefault (word/line delete, select all, format, undo/redo, Ctrl+O) also ran when an input embedded in a node's DOM had the focus, so eg. Ctrl+Backspace or Cmd+A did nothing in it. Skip them when the keydown comes from such an input, as copy and cut already do. Other controls in a decorator, such as a button, keep the editor's shortcuts. Co-authored-by: Ved Thakar <111605341+Vedthakar@users.noreply.github.com> Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-authored-by: Ved Thakar <111605341+Vedthakar@users.noreply.github.com> Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
0ce5803 to
316f388
Compare
|
Thanks for the review. Fixed in the latest push, which also resolves the conflict with
Added a changeset. The browser test file passes in Chromium (15 tests). — Ved Thakar |
There was a problem hiding this comment.
Thanks for picking up #3761, and for limiting the skip so a <button> inside a decorator keeps the editor's shortcuts. Two things need fixing before this can merge:
-
Non-text inputs still lose the editor's undo, redo and select all (
packages/lexical/src/LexicalEvents.ts:2132, and the same check at:2216and:2253).$isEventFromEmbeddedInputis true for any focused<input>, whatever itstype, so a checkbox, radio, range or color input inside a decorator now swallows Mod+Z, Mod+Shift+Z, Ctrl+Y and Mod+A without doing anything. To reproduce in the playground: insert a poll, vote by clicking an option's checkbox, then press Ctrl+Z. Onmainthe vote is undone; with this change nothing happens. The test's own rule ("a button has no action of its own for it") applies to these inputs too. Restricting the skip to text-entry targets (a<textarea>, a foreign contentEditable, or an<input>whosetypeis notbutton,checkbox,color,file,image,radio,range,resetorsubmit) keeps the fix, and a checkbox case in the browser test would cover it. If you add a helper for this, keep any lookup table inside the function rather than at module scope (see "Tree-shaking annotations" in AGENTS.md). -
The new test fails the TypeScript check (
packages/lexical/src/__tests__/browser/Issue3761DecoratorInputShortcuts.test.ts:139).getFirstDescendantno longer takes a type argument onmain, sotsc -p ./tsconfig.test.json(part ofpnpm run ci-check) reports TS2558 and TS2339 on that line, and thecore-tests / integrityjob is red. Narrowing works instead:const text = $getRoot().getFirstDescendant(); assert($isTextNode(text)); text.select(6, 6);
(import
$isTextNodeand drop the now-unusedTextNodetype import).
Optional, non-blocking:
- The changeset could carry a
Verify:paragraph, since the fix changes editing behaviour (undo in a decorator's input now acts on the input). CONTRIBUTING.md ("Changesets") and.changeset/README.mdask for one on such a fix, for example:Verify: keyboard shortcuts (Mod+Z, Mod+A, Ctrl/Alt+Backspace, Mod+B) pressed in an input or textarea inside a decorator; the editor's undo and formatting no longer run for them. - The Test plan still describes an earlier version of the branch (Mod+C/Mod+X cases, a helper rename, 16/18 tests, a clean
tsc). AGENTS.md ("Commits and Pull Requests") asks for the real Before and After output, so it would help to re-run the test and paste the output for the current file.
getFirstDescendant no longer takes a type argument on main. Co-authored-by: Ved Thakar <111605341+Vedthakar@users.noreply.github.com> Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
$isEventFromEmbeddedInput is true for any <input>, so a checkbox, radio or range input in a decorator swallowed undo, redo and select all without doing anything with them. Skip the editor's shortcut only for an input that takes text. Add a checkbox case to the browser test and a Verify paragraph to the changeset. Co-authored-by: Ved Thakar <111605341+Vedthakar@users.noreply.github.com> Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
|
Thanks, both fixed in the latest push:
Also added the — Ved Thakar |
There was a problem hiding this comment.
Thanks for working through #3761. The core of the fix works: in the playground, Ctrl+Backspace in a poll option now deletes the word in the input instead of sending DELETE_WORD_COMMAND to the editor, and the new browser tests fail without the LexicalEvents.ts change and pass with it. One issue needs fixing before this can land.
Ctrl+Z in a decorator input can now undo the editor's text natively in Chromium (packages/lexical/src/LexicalEvents.ts, the new early return at line 2153 that the undo/redo shortcuts go through)
Chromium keeps one undo stack per page. When the focused decorator input has no edit of its own on top of that stack, Ctrl+Z makes Chromium undo the user's last typing in the editor and fire beforeinput (historyUndo) at the editor root. onBeforeInput skips that event because the focused element is a decorator input ($isInputEventTargetingCapturedSelection, the active-element fallback at lines 1218-1223), so nothing calls preventDefault(), and the historyUndo handling in $handleBeforeInput (lines 1394-1416) never runs. That branch exists, per its #6714 comment, so the browser's native history never rewrites the editor's DOM behind its back; with this change that rewrite becomes reachable from the keyboard.
Steps in the playground (Chromium):
- Type a line in the editor, then insert a poll.
- Click at the end of the first paragraph and type
and more. - Click into the empty "Option 2" input and press Ctrl+Z.
On this branch and more disappears with no UNDO_COMMAND (the tree view logs an INPUT_COMMAND), Redo stays disabled, focus moves out of the poll into the editor, and pressing Ctrl+Z again brings the text back. Firefox keeps undo per control and is not affected.
Suggested direction: in onBeforeInput, decide historyUndo/historyRedo by their composed target. When it is the editor root or Lexical-managed content rather than the focused control, prevent the default (and either dispatch UNDO_COMMAND/REDO_COMMAND, as the #6714 path does for a control outside the editor, or do nothing), and keep the active-element fallback for other input types and for Firefox's retargeting (#8738). A browser test with real key presses would cover it: type in the editor with userEvent, focus the decorator input, press Mod+Z, then assert the editor's text and that focus stays in the input. The current Mod+Z case dispatches a synthetic keydown, which cannot show what the browser's own undo does.
Optional, non-blocking:
- Once undo is handled, re-read the changeset's
Verify:line ("the editor's undo and formatting no longer run for them"); in Chromium it does not hold yet (CONTRIBUTING.md, "Changesets", asks theVerify:paragraph to say what to test).
| key, | ||
| modifiers, | ||
| onMatch: (event, editor) => { | ||
| if ($isEventFromEmbeddedTextInput(event)) { |
There was a problem hiding this comment.
For the undo/redo entries this hands Ctrl+Z to the browser, and in Chromium the browser's undo stack is per page: when this input has no edit of its own on top of it, Chromium undoes the user's last typing in the editor and fires beforeinput historyUndo at the editor root. onBeforeInput skips that event because the focused element is a decorator input (the active-element fallback in $isInputEventTargetingCapturedSelection, lines 1218-1223), so the #6714 branch of $handleBeforeInput (lines 1394-1416) never prevents it. In the playground: type in the editor, click into an empty poll option, press Ctrl+Z: the editor text changes with no UNDO_COMMAND, focus leaves the poll, and the next Ctrl+Z restores it. Deciding historyUndo/historyRedo by their composed target in onBeforeInput (prevent when it is the editor root, keep the fallback for Firefox's #8738 retargeting) would close it.
| input.focus(); | ||
| const spy = spyOn(editor, command); | ||
| const event = keyDown(input, init); | ||
| expect(event.defaultPrevented).toBe(false); |
There was a problem hiding this comment.
For Mod+Z this synthetic keydown passes even though Chromium's real undo then rewrites the editor (see the comment on LexicalEvents.ts line 2153). A real-key case like the two at the end of the file, typing in the editor first and then pressing Mod+Z in the input, asserting the editor's text and the focused element, would cover it.
Description
This solves issue #3761: some shortcuts do nothing in an input inside a decorator node, for example Ctrl+Delete / Ctrl+Backspace in the playground's poll inputs, or Cmd+A on macOS.
Current behavior. The editor's keydown listener on the root also gets keydowns from a decorator's own
<input>. Its shortcut table runs them as editor shortcuts. Theprevent(...)shortcuts and select all callevent.preventDefault()before dispatching their command:So the browser never deletes the word or selects the input's text, and the command acts on the editor's selection instead.
beforeinputandinputalready skip a decorator's captured input (#8740, viaisInputEventTargetingCapturedSelection), but keydown didn't.What this changes.
preventDefault()or dispatching when the keydown comes from a text input embedded in a node's DOM: a<textarea>, a foreign contentEditable, or an<input>of a type the user types into. This builds on$isEventFromEmbeddedInput, which [lexical-plain-text][lexical-rich-text] Bug Fix: Leave a copy, cut or paste in a decorator's input to the input #9288 added for copy, cut and paste, and excludes the<input>types that take no text (button,checkbox,color,file,image,radio,range,reset,submit).<button>or a checkbox, keep the editor's shortcuts: the browser has no action of its own for them there.mainsince [lexical-plain-text][lexical-rich-text] Bug Fix: Leave a copy, cut or paste in a decorator's input to the input #9288, so this PR does not touch them.<input>on the page.Closes #3761
Test plan
New browser test
packages/lexical/src/__tests__/browser/Issue3761DecoratorInputShortcuts.test.ts, with a block decorator holding an<input>:defaultPreventedand doesn't dispatch the command.<button>or a checkbox inside the decorator is stilldefaultPreventedand dispatchesUNDO_COMMAND.defaultPreventedand dispatches its command once.hello worldbecomeshello, and the editor is unchanged.Before
On current
mainwith the test file applied andLexicalEvents.tsfrommain(Chromium, macOS):After
On this branch (Chromium, macOS):
tsc,tsc-test, eslint and prettier on the changed files are clean. Firefox and WebKit were not run locally.Author: Ved Thakar (@Vedthakar)
🤖 Generated with Claude Code